#include "headfiles.h"

using namespace std;

int calcWordFreq(const char *book_name, const string &word) {
    if (book_name == NULL) {
        cout << "Invalid book name, NULL!" << endl;
        return -1;
    }

    ifstream ifs;
    ifs.open(book_name, fstream::in);
    if (!ifs.is_open()) {
        cout << "Book " << book_name << " open error!" << endl;
        return -1;
    }

    string w;
    map<string, int> word_count;
    map<string, int>::iterator it;
    while (ifs >> w) {
        if (w.empty()) {
            break;
        }

        it = word_count.find(w);
        if (it == word_count.end()) {
            word_count[w] = 1;
        }
        else {
            word_count[w]++;
        }
    }

    it = word_count.find(word);
    if (it == word_count.end()) {
        return -1;
    }
    else {
        return it->second;
    }
}

